package Jvm;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by kai on 2015/8/16 0016.
 */
public class VolatileTestZuo {
    //线程不安全
    public static volatile int count = 0;
    //线程安全
    public static AtomicInteger race = new AtomicInteger(0);

    public static void increase() {
        count++;
    }

    public static void increaseAtom() {
        race.getAndIncrement();
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000; i++)
//                        increase();
                        increaseAtom();
                }
            }
            ).start();
        } 

        //t
//        while (Thread.activeCount() > 1) {
//            Thread.yield();
//        }
        Thread.sleep(1000l);
//        System.out.println(count);
        System.out.println(race.get());
    }
}
